// Service Worker for Chrome Extension
console.log('Background script loaded');

// 插件安装时的处理
chrome.runtime.onInstalled.addListener(function(details) {
  console.log('Extension installed:', details);
  
  // 设置默认存储数据
  chrome.storage.sync.set({
    extensionEnabled: true,
    lastUsed: Date.now()
  });
});

// 监听来自popup或content script的消息
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  console.log('Received message:', request);
  
  if (request.action === 'getData') {
    // 从存储中获取数据
    chrome.storage.sync.get(['extensionEnabled'], function(result) {
      sendResponse({enabled: result.extensionEnabled});
    });
    return true; // 保持消息通道开放
  }
  
  if (request.action === 'setData') {
    // 保存数据到存储
    chrome.storage.sync.set(request.data, function() {
      sendResponse({success: true});
    });
    return true;
  }
});

// 标签页更新时的处理
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (changeInfo.status === 'complete' && tab.url) {
    console.log('Tab updated:', tab.url);
  }
});